//
//  MainViewController.m
//  A段项目,漫画
//
//  Created by dllo on 15/4/20.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MainViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MainViewController ()
@property(nonatomic, retain)HomeViewController *homeVC;
@property(nonatomic, retain)ClassViewController *classVC;
@property(nonatomic, retain)SaveViewController *saveVC;
@property(nonatomic, retain)TopicViewController *topicVC;
@property(nonatomic, retain)beginAnimationViewController *beginVC;
@property(nonatomic, retain)UINavigationController *homeNA;
@property(nonatomic, assign)BOOL isFirstStart;
//清除缓存
@property(nonatomic, retain)UIView *witeView;
@property(nonatomic, retain)UIAlertView *wipeAlert;
@property(nonatomic, retain)UIView *wipeView;
@property(nonatomic, retain)UIButton *startWipe;
@property(nonatomic, retain)UIProgressView *wipeProgress;
@property(nonatomic, retain)UILabel *currentLabel;
@property(nonatomic, retain)UIButton *cancelWipe;
@property(nonatomic, assign)NSInteger fileCount;
@property(nonatomic, retain)UIAlertView *finishAlert;
//搜索提示
@property(nonatomic, retain)UITableView *searchTable;
//工具栏搜索提示
@property(nonatomic, retain)UITableView *toolsSearchTable;
//浏览记录
@property(nonatomic, retain)UITableView *throughMark;
@property(nonatomic, retain)NSMutableArray *throughArr;
@property(nonatomic, retain)UIImageView *downButton;
@property(nonatomic, retain)UINavigationController *currentNavigationcontroller;
//搜索
@property(nonatomic, retain)UIView *searchView;
@property(nonatomic, retain)UIButton *cancelSearch;
@property(nonatomic, retain)UITextField *searchTextField;
@property(nonatomic, retain)UIButton *searchButton;
//关于我们
@property(nonatomic, retain)UIView *aboutUsView;
@property(nonatomic, retain)UILabel *aboutUsLabel;
@end

@implementation MainViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.isFirstStart = YES;
        [MidTools shareMidTools].delegate4 = self;
        [MidTools shareMidTools].delegate6 = self;
        //刷新数据
        [MidTools shareMidTools].isSearchMarkShow = NO;
        //显示浏览记录
        [MidTools shareMidTools].delegate9 = self;
        self.throughArr = [NSMutableArray array];
        //
        //页面跳转
        [MidTools shareMidTools].delegate1 = self;
        //搜索
        [MidTools shareMidTools].delegate5 = self;
    }
    return self;
}
//代理搜索跳转
- (void)searchCartoon{
    //先关抽屉
    [[MidTools shareMidTools].delegate2 closeTool];
    //弹出搜索框
    [UIView animateWithDuration:0.5 animations:^{
        [self.view addSubview:self.searchView];
        [self.toolsSearchTable reloadData];
        self.searchView.frame = CGRectMake(WIDTH / 2 - WIDTH / 4, HEIGHT / 2 - HEIGHT / 4, WIDTH / 2, HEIGHT /  2);
        self.cancelSearch.frame = CGRectMake(WIDTH / 2 - 40, 5, WIDTH / 15, WIDTH / 15);
        self.searchTextField.frame = CGRectMake(10, 40, WIDTH / 2 - 20, 30);
        self.searchButton.frame = CGRectMake(5, 0, WIDTH / 15, WIDTH / 15);
        self.toolsSearchTable.frame = CGRectMake(10, 80, WIDTH / 2 - 20, HEIGHT /  2 - 110);
        //指明操作者
        [MidTools shareMidTools].searchID = 3;
    } completion:^(BOOL finished) {
    }];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
//创建搜索视图
- (void)createSearch{
    self.searchView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH / 2 - WIDTH / 4, HEIGHT / 2 - HEIGHT / 4, 0, HEIGHT /  2)];
    [self.view addSubview:self.searchView];
    self.searchView.backgroundColor = [UIColor whiteColor];
    self.searchView.alpha = 0.8;
    [self.searchView release];
    self.searchView.layer.borderWidth = 2;
    self.searchView.layer.cornerRadius = 20;
    self.searchView.layer.borderColor = [UIColor clearColor].CGColor;
    //按钮
    self.cancelSearch = [UIButton buttonWithType:UIButtonTypeSystem];
    self.cancelSearch.frame = CGRectMake(WIDTH / 2 - 40, 5, 0, WIDTH / 15);
    [self.cancelSearch setBackgroundImage:[UIImage imageNamed:@"iconfont-crmtubiao42"] forState:UIControlStateNormal];
    [self.cancelSearch addTarget:self action:@selector(cancelSearchAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.searchView addSubview:self.cancelSearch];
    //搜索框
    self.searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 40, 0, 30)];
    [self.searchView addSubview:self.searchTextField];
    self.searchTextField.layer.borderWidth = 1;
    self.searchTextField.layer.cornerRadius = 10;
    // 159 163 175
    self.searchTextField.layer.borderColor = [UIColor colorWithRed:159 / 255.0 green:163 / 255.0 blue:175 / 255.0 alpha:1].CGColor;
    self.searchTextField.delegate = self;
    self.searchButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.searchButton setBackgroundImage:[UIImage imageNamed:@"iconfont-sousuodawd"] forState:UIControlStateNormal];
    self.searchButton.frame = CGRectMake(5, 0, 0, WIDTH / 15);
    self.searchTextField.rightView = self.searchButton;
    self.searchTextField.rightViewMode = UITextFieldViewModeAlways;
    [self.searchButton addTarget:self action:@selector(startSearchAction:) forControlEvents:UIControlEventTouchUpInside];
    //tableview
    self.toolsSearchTable = [[UITableView alloc] initWithFrame:CGRectMake(10, 80, 0, HEIGHT /  2 - 110) style:UITableViewStyleGrouped];
    [self.searchView addSubview:self.toolsSearchTable];
    [self.toolsSearchTable release];
    self.toolsSearchTable.layer.borderWidth = 1;
    self.toolsSearchTable.layer.cornerRadius = 10;
    self.toolsSearchTable.layer.borderColor = [UIColor colorWithRed:159 / 255.0 green:163 / 255.0 blue:175 / 255.0 alpha:1].CGColor;
    self.toolsSearchTable.backgroundColor = [UIColor whiteColor];
    self.toolsSearchTable.alpha = 0.9;
    self.toolsSearchTable.delegate = self;
    self.toolsSearchTable.dataSource = self;
}
- (void)startSearchAction:(UIButton *)button{
    if (![self.searchTextField.text isEqual:@""]) {
        [self.searchTextField resignFirstResponder];
        //用单例进行搜索
        NSString *input = self.searchTextField.text;
        NSLog(@"%@",input);
        NSString *str = [input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
        NSString *urlStr = [NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Search/comicSearchListForIos/key_word/%@/listcnt/12/page/1/clientType/0/local_version/2.3.2",str];
        [[MidTools shareMidTools] searchByUrl:urlStr];
        [self.searchTextField resignFirstResponder];
        [self cancelSearchAction:[[UIButton alloc] init]];
        //推
        SearchDetalisViewController *searchDL = [[SearchDetalisViewController alloc] init];
        [self.currentNavigationcontroller pushViewController:searchDL animated:YES];
        [searchDL release];
        //存储搜索记录
        //先判断是否有相同记录
        BOOL isExit = NO;
        for (NSString *s in [MidTools shareMidTools].searchMark) {
            if ([s isEqual:self.searchTextField.text]) {
                isExit = YES;
            }
        }
        if (!isExit) {
            NSString *searchMark = self.searchTextField.text;
            [[MidTools shareMidTools].searchMark addObject:searchMark];
            self.searchTextField.text = nil;
        }
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"搜索关键字不能为空" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
        [alert release];
    }
    self.searchTextField.text = nil;
}
//回收搜索栏
- (void)cancelSearchAction:(UIButton *)button{
    [UIView animateWithDuration:0.5 animations:^{
        [self.searchView removeFromSuperview];
        self.searchView.frame = CGRectMake(WIDTH / 2 - WIDTH / 4, HEIGHT / 2 - HEIGHT / 4, 0, HEIGHT /  2);
        self.cancelSearch.frame = CGRectMake(WIDTH / 2 - 40, 5, 0, WIDTH / 15);
        self.searchTextField.frame = CGRectMake(10, 40, 0, 30);
        self.searchButton.frame = CGRectMake(5, 0, 0, WIDTH / 15);
        self.toolsSearchTable.frame = CGRectMake(10, 80, 0, HEIGHT /  2 - 110);
    } completion:^(BOOL finished) {
        
    }];
}
//关于我们
- (void)createViewAboutUs{
    self.aboutUsView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.aboutUsView.center = CGPointMake(WIDTH / 2, HEIGHT / 2);
    self.aboutUsView.backgroundColor = [UIColor clearColor];
    
    self.aboutUsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 2, HEIGHT / 2)];
    self.aboutUsLabel.center = CGPointMake(WIDTH / 2, HEIGHT / 2);
    [self.aboutUsView addSubview:self.aboutUsLabel];
    [self.aboutUsLabel release];
    self.aboutUsLabel.layer.borderWidth = 1.5;
    self.aboutUsLabel.layer.cornerRadius = 20;
    self.aboutUsLabel.layer.borderColor = [UIColor clearColor].CGColor;

    
    
}
//关于我们点击
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.aboutUsView removeFromSuperview];
}
//PUSH页面
- (void)pushWithIndex:(NSInteger)index{
    if (index == 6) {
        AboutUSViewController *abVC = [[AboutUSViewController alloc] init];
        [self.currentNavigationcontroller pushViewController:abVC animated:YES];
        [abVC release];
    }
}
//显示浏览记录
- (void)showThrough{
    [UIView animateWithDuration:1 animations:^{
        self.throughMark.frame = CGRectMake(0, HEIGHT / 2 - HEIGHT / 1.5 / 2, WIDTH / 1.5, HEIGHT / 1.5);
        //刷新数据
        [self.throughMark reloadData];
    } completion:^(BOOL finished) {
//        [self.view addSubview:self.downButton];
    }];
}
//浏览记录
- (void)createThroughMark{
    self.throughMark = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT / 2 - HEIGHT / 1.5 / 2, 0, HEIGHT / 1.5) style:UITableViewStyleGrouped];
    [self.view addSubview:self.throughMark];
    [self.throughMark release];
    self.throughMark.delegate = self;
    self.throughMark.dataSource = self;
    self.throughMark.backgroundColor = [UIColor whiteColor];
    self.throughMark.alpha = 0.9;
    self.throughMark.layer.borderWidth = 1;
    self.throughMark.layer.cornerRadius = 10;
    self.throughMark.layer.borderColor = [UIColor clearColor].CGColor;
    self.throughMark.rowHeight = 80;
    self.throughMark.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 1.5, 40)];
    self.throughMark.tableHeaderView = headView;
    [headView release];
//    //按钮
    UIImage *m = [UIImage imageNamed:@"iconfont-xiangzuoawd"];
    self.downButton = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH / 1.5 - 40, 5, m.size.width, m.size.height)];
    
    self.downButton.image = m;
    [headView addSubview:self.downButton];
    self.downButton.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(downButtonTap:)];
    [self.downButton addGestureRecognizer:tap];
    
}
//回收浏览记录
- (void)downButtonTap:(UITapGestureRecognizer *)tap{
    [UIView animateWithDuration:0.5 animations:^{
        self.throughMark.frame = CGRectMake(0, HEIGHT / 2 - HEIGHT / 1.5 / 2, 0, HEIGHT / 1.5);
    } completion:^(BOOL finished) {
    }];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if ([tableView isEqual:self.throughMark]) {
        return @"浏览记录";
    }else{
        return @"";
    }
}



- (void)reloadMark{
    [self.searchTable reloadData];
}

- (void)createSearchTable{
    self.searchTable = [[UITableView alloc] initWithFrame:CGRectMake((WIDTH - 200) / 2, 55, 200, 0) style:UITableViewStylePlain];
    self.searchTable.layer.borderColor = [UIColor colorWithRed:128 / 255.0 green:128 / 255.0 blue:128 / 255.0 alpha:1].CGColor;
    [self.view addSubview:self.searchTable];
    [self.searchTable release];
    self.searchTable.delegate = self;
    self.searchTable.dataSource = self;
    self.searchTable.backgroundColor = [UIColor whiteColor];
    self.searchTable.alpha = 0.7;
    self.searchTable.layer.borderWidth = 1.5;
    self.searchTable.layer.cornerRadius = 10;
}
- (void)makeSearchMarkWithNow:(BOOL)isShow{
    if (isShow) {
        //隐藏
        [UIView animateWithDuration:0.2 animations:^{
            self.searchTable.frame = CGRectMake(WIDTH / 4, 55, WIDTH / 2, 0);
            [MidTools shareMidTools].isSearchMarkShow = NO;
        } completion:^(BOOL finished) {
        }];
    }else{
        //显示
        [UIView animateWithDuration:0.2 animations:^{
            self.searchTable.frame = CGRectMake(WIDTH / 4, 55, WIDTH / 2, 120);
            //之后刷新数据
            [self.searchTable reloadData];
            [MidTools shareMidTools].isSearchMarkShow = YES;
        } completion:^(BOOL finished) {
        }];
    }
}
//tableview点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.searchTable]) {
        //点击事件
        NSString *mark = [MidTools shareMidTools].searchMark[indexPath.row];
        //传值
        if ([MidTools shareMidTools].searchID == 1) {
            [[MidTools shareMidTools].delegate81 getValue1:mark];
        }
        if ([MidTools shareMidTools].searchID == 2) {
            [[MidTools shareMidTools].delegate82 getValue2:mark];
        }
    }
    if ([tableView isEqual:self.throughMark]) {
        NSLog(@"PUSH");
        //先收回浏览记录
        [self downButtonTap:[[UITapGestureRecognizer alloc] init]];
        
        ReadCartoonViewController *readVC = [[ReadCartoonViewController alloc] init];
        //PUSH之前隐藏tabbar
        readVC.hidesBottomBarWhenPushed = YES;
        
        throughNotes *tn = self.throughArr[self.throughArr.count - indexPath.row - 1];
        readVC.comic_id = tn.comic_id;
        readVC.cid = tn.cid;
        readVC.TiTle = tn.title;
        readVC.PicUrl = tn.picURL;
        readVC.picIndex = tn.lastPage;
        [self.currentNavigationcontroller pushViewController:readVC animated:YES];
        [readVC release];
    }
    if ([tableView isEqual:self.toolsSearchTable]) {
        NSLog(@"asd");
        if ([MidTools shareMidTools].searchID == 3) {
            NSLog(@"awd");
            NSString *mark = [MidTools shareMidTools].searchMark[indexPath.row];
            self.searchTextField.text = mark;
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //搜索记录
    if ([tableView isEqual:self.searchTable]) {
        return [MidTools shareMidTools].searchMark.count;
    }else if([tableView isEqual:self.throughMark]){
        //浏览记录
        [[DataBaseHandle shareDB] openDB];
        [[DataBaseHandle shareDB] createThroughTable];
        self.throughArr = [[DataBaseHandle shareDB] selectThroughNotes];
        return self.throughArr.count;
    }else if([tableView isEqual:self.toolsSearchTable]){
        //搜索记录
        return [MidTools shareMidTools].searchMark.count;
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.searchTable]) {
        //搜索历史
        static NSString *reuse = @"reuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
        }
        NSString *mark = [MidTools shareMidTools].searchMark[indexPath.row];
        cell.textLabel.text = mark;
        return cell;
    }else if([tableView isEqual:self.throughMark]){
        //浏览记录
        static NSString *reuse1 = @"reuse1";
        throughNotesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse1];
        if (cell == nil) {
            cell = [[[throughNotesTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse1] autorelease];
        }
        throughNotes *tn = self.throughArr[self.throughArr.count - indexPath.row - 1];
        [cell.headImage sd_setImageWithURL:[NSURL URLWithString:tn.picURL]];
        cell.titleLabel.text = tn.title;
        cell.throughMark.text = [NSString stringWithFormat:@"最近浏览:%@话  %ld页",tn.cid,tn.lastPage];
        return cell;
    }else if([tableView isEqual:self.toolsSearchTable]){
        //还是搜索记录
        static NSString *reuse = @"reuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
        }
        NSString *mark = [MidTools shareMidTools].searchMark[indexPath.row];
        cell.textLabel.text = mark;
        return cell;
    }else{
        //空
        static NSString *reuse = @"reuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
        }
        return cell;
    }
}
//清除缓存
- (void)createWipeView{
    self.wipeView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH, HEIGHT / 2, WIDTH / 1.3, WIDTH / 1.3 / 2)];
    
    [self.view addSubview:self.wipeView];
    [self.wipeView release];
    self.wipeView.backgroundColor = [UIColor colorWithRed:128 / 255.0 green:128 / 255.0 blue:128 / 255.0 alpha:1];
//    self.wipeView.alpha = 0.9;
    self.wipeView.layer.borderWidth = 5;
    self.wipeView.layer.cornerRadius = 20;
    self.wipeView.layer.borderColor = [UIColor clearColor].CGColor;
    //开始按钮
    self.startWipe = [UIButton buttonWithType:UIButtonTypeSystem];
    self.startWipe.frame = CGRectMake(20, 10, WIDTH / 4.7, WIDTH / 4.7 / 2);
    [self.wipeView addSubview:self.startWipe];
    self.startWipe.layer.borderWidth = 1;
    self.startWipe.layer.cornerRadius = 5;
    [self.startWipe setTitle:@"清除缓存" forState:UIControlStateNormal];
    [self.startWipe addTarget:self action:@selector(wipeCache:) forControlEvents:UIControlEventTouchUpInside];
    
    self.wipeProgress = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    self.wipeProgress.frame = CGRectMake(WIDTH / 1.3 / 6, 10 + WIDTH / 4.7 / 2 + 10, WIDTH / 1.9, 30);
    [self.wipeView addSubview:self.wipeProgress];
    [self.wipeProgress release];
    
    self.currentLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 1.3 / 6, 10 + WIDTH / 4.7 / 2 + 5 + 20, WIDTH / 1.9, 40)];
    [self.wipeView addSubview:self.currentLabel];
    self.currentLabel.layer.borderWidth = 1;
    self.currentLabel.layer.cornerRadius = 5;
    [self.currentLabel release];
    self.currentLabel.textAlignment = NSTextAlignmentCenter;
    
    
    self.cancelWipe = [UIButton buttonWithType:UIButtonTypeSystem];
    UIImage *image = [UIImage imageNamed:@"iconfont-quxiao.png"];
    self.cancelWipe.frame = CGRectMake(WIDTH / 1.3 - 40, 10, image.size.width, image.size.height);
    [self.cancelWipe setBackgroundImage:image forState:UIControlStateNormal];
    [self.wipeView addSubview:self.cancelWipe];
    [self.cancelWipe addTarget:self action:@selector(cancelWipeAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createViewAboutUs];
    [self createView];
    [self createWipeView];
    [self createSearchTable];
    [self createThroughMark];
    [self createSearch];
}

- (void)createView{
    
    self.homeVC = [[HomeViewController alloc] init];
    self.homeNA = [[UINavigationController alloc] initWithRootViewController:self.homeVC];
    [self.homeVC release];
    UIImage *homeImage = [UIImage imageNamed:@"iconfont-zhuye"];
    self.homeVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"主页" image:homeImage tag:1001];
    
    
    self.classVC = [[ClassViewController alloc] init];
    UINavigationController *classNA = [[UINavigationController alloc] initWithRootViewController:self.classVC];
    [self.classVC release];
    UIImage *classImage = [UIImage imageNamed:@"iconfont-fenlei"];
    self.classVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"分类" image:classImage tag:1002];
    
    self.saveVC = [[SaveViewController alloc] init];
    UINavigationController *saveNA = [[UINavigationController alloc] initWithRootViewController:self.saveVC];
    [self.saveVC release];
    UIImage *saveImage = [UIImage imageNamed:@"iconfont-shoucang-2"];
    self.saveVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"收藏" image:saveImage tag:1003];
    
    self.topicVC = [[TopicViewController alloc] init];
    UINavigationController *topicNA = [[UINavigationController alloc] initWithRootViewController:self.topicVC];
    [self.topicVC release];
    UIImage *topicImage = [UIImage imageNamed:@"iconfont-play"];
    self.topicVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"动画" image:topicImage tag:1004];
    
    
    UITabBarController *cartoonTabbar = [[UITabBarController alloc] init];
    cartoonTabbar.viewControllers = [NSArray arrayWithObjects:self.homeNA, classNA, saveNA, topicNA, nil];
    cartoonTabbar.delegate = self;
    
    self.currentNavigationcontroller = self.homeNA;
    
    [self addChildViewController:cartoonTabbar];
    [self.view addSubview:cartoonTabbar.view];
}
//移走缓存视图
- (void)cancelWipeAction:(UIButton *)button{
    [UIView animateWithDuration:1 animations:^{
        self.wipeView.frame = CGRectMake(WIDTH, HEIGHT / 2, WIDTH / 1.3, WIDTH / 1.3 / 2);
    } completion:^(BOOL finished) {
    }];
}
- (void)wipeCache:(UIButton *)button{
    NSArray *sandBoxCache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *sandBoxCachePath = [sandBoxCache lastObject];
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:sandBoxCachePath];
    self.fileCount = files.count;
    //删除文件
    for (NSString *p in files) {
        NSError *error;
        NSString *path = [sandBoxCachePath stringByAppendingPathComponent:p];
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
            [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
            NSArray *Wipefiles = [[NSFileManager defaultManager] subpathsAtPath:sandBoxCachePath];
            self.wipeProgress.progress = (self.fileCount - Wipefiles.count) / (self.fileCount + 0.1);
            self.currentLabel.text = [NSString stringWithFormat:@"缓存文件数量: %ld",Wipefiles.count];
            NSLog(@"%g",self.wipeProgress.progress);
        }
    }
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(finishWipe) userInfo:nil repeats:NO];
}
- (void)finishWipe{
    self.finishAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"缓存清理完成" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [self.finishAlert show];
    [self.finishAlert release];
    self.finishAlert.delegate = self;
    self.wipeProgress.progress = 0;
}
- (void)wipe{
    self.wipeAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定要清除缓存吗" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [self.wipeAlert show];
    self.wipeAlert.delegate = self;
    [self.wipeAlert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ([alertView isEqual:self.wipeAlert]) {
        if (buttonIndex == 1) {
            //显示
            [UIView animateWithDuration:1 animations:^{
                [self.view bringSubviewToFront:self.wipeView];
                self.wipeView.center = CGPointMake(WIDTH / 2, HEIGHT / 2 + 10);
            } completion:^(BOOL finished) {
                NSArray *sandBoxCache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
                NSString *sandBoxCachePath = [sandBoxCache lastObject];
                NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:sandBoxCachePath];
                self.fileCount = files.count;
                self.currentLabel.text = [NSString stringWithFormat:@"缓存文件数量: %ld",self.fileCount];
            }];
        }
    }else if ([alertView isEqual:self.finishAlert]){
        NSLog(@"完成点击");
        [UIView animateWithDuration:1 animations:^{
            self.wipeView.frame = CGRectMake(WIDTH, HEIGHT / 2, WIDTH / 1.3, WIDTH / 1.3 / 2);
        } completion:^(BOOL finished) {
        }];
    }
    
}
- (void)toNewVC{
    NewViewController *newVC = [[NewViewController alloc] init];
    [self.navigationController pushViewController:newVC animated:YES];
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    NSInteger index = tabBarController.selectedIndex;
    switch (index) {
        case 0:
            self.currentNavigationcontroller = self.homeNA;
            [self.homeVC.navigationController popToViewController:self.homeVC.navigationController.viewControllers[0] animated:YES];
            if ([MidTools shareMidTools].isSearchMarkShow) {
                self.searchTable.frame = CGRectMake((WIDTH - 200) / 2, 55, 200, 0);
                [MidTools shareMidTools].isSearchMarkShow = NO;
            }
            [self cancelSearchAction:[[UIButton alloc] init]];
            //放弃第一响应者
            [[MidTools shareMidTools].delegate71 resignFirst1];
            [[MidTools shareMidTools].delegate72 resignFirst2];
            break;
        case 1:
            self.currentNavigationcontroller = self.classVC.navigationController;
            NSLog(@"%ld",index);
            [self cancelSearchAction:[[UIButton alloc] init]];
            //放弃第一响应者
            [[MidTools shareMidTools].delegate71 resignFirst1];
            [[MidTools shareMidTools].delegate72 resignFirst2];
            break;
        case 2:
            self.currentNavigationcontroller = self.saveVC.navigationController;
            [self.saveVC.navigationController popToViewController:self.saveVC.navigationController.viewControllers[0] animated:YES];
            NSLog(@"%ld",index);
            if ([MidTools shareMidTools].isSearchMarkShow) {
                self.searchTable.frame = CGRectMake((WIDTH - 200) / 2, 55, 200, 0);
                [MidTools shareMidTools].isSearchMarkShow = NO;
            }
            [self cancelSearchAction:[[UIButton alloc] init]];
            //放弃第一响应者
            [[MidTools shareMidTools].delegate71 resignFirst1];
            [[MidTools shareMidTools].delegate72 resignFirst2];
            break;
        case 3:
            self.currentNavigationcontroller = self.topicVC.navigationController;
            NSLog(@"%ld",index);
            if ([MidTools shareMidTools].isSearchMarkShow) {
                self.searchTable.frame = CGRectMake((WIDTH - 200) / 2, 55, 200, 0);
                [MidTools shareMidTools].isSearchMarkShow = NO;
            }
            [self cancelSearchAction:[[UIButton alloc] init]];
            //放弃第一响应者
            [[MidTools shareMidTools].delegate71 resignFirst1];
            [[MidTools shareMidTools].delegate72 resignFirst2];
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
